﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CMSModel;
using System.IO;
using System.Text;

public partial class Advertise_Add : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        CMSModel.Advertise advertise = new CMSModel.Advertise();
        advertise.AdvertiseId = txtId.Text;
        advertise.link = txtLink.Text;
        string path = Server.MapPath("~/AdvertiseUpload/");
        Boolean fileOk = false;
        //判断是否已经选取文件
        if (flImgSwf.HasFile == true)
        {
            //取得文件的扩展名,并转换成小写
            string fileExtension = System.IO.Path.GetExtension(this.flImgSwf.FileName).ToLower();
            //限定只能上传图片类型
            Dictionary<string, string> myDic = new Dictionary<string, string>();
            myDic.Add("1", ".jpg");
            myDic.Add("2", ".gif");
            myDic.Add("3", ".jpeg");
            myDic.Add("4", ".bmp");
            myDic.Add("5", ".png");
            myDic.Add("6", ".swf");
            int width = 270;
            if (rdo1.Checked)
                width = 270;
            else if (rdo2.Checked)
                width = 680;
            else if (rdo3.Checked)
                width = 720;
            else
                width = 230;
            if (myDic.ContainsValue(fileExtension))
            {
                fileOk = true;
            }
            if (fileOk)
            {
                try
                {
                    string fileName = "advertise" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + "_" + txtId.Text + fileExtension;
                    string temp = path + fileName;
                    this.flImgSwf.PostedFile.SaveAs(temp);
                    StringBuilder sb = new StringBuilder();
                    if (fileExtension == ".swf")
                    {
                        sb.AppendLine("<object width=\""+width+"\" height=\"60\" style=\"float: left\"><param name=\"movie\" value=\"" + temp + "\"></param>");
                        sb.AppendLine("<param name=\"allowScriptAccess\" value=\"sameDomain\"></param>");
                        sb.AppendLine("<param name=\"wmode\" value=\"transparent\"></param>");
                        sb.AppendLine("<embed src=\"" + temp + "\" type=\"application/x-shockwave-flash\" width=\"" + width + "\" height=\"60\" allowfullscreen=\"true\" wmode=\"transparent\" allowscriptaccess=\"always\"></embed></object>");
                    }
                    else
                        sb.AppendLine("<a href=\"" + txtLink.Text + "\"><img src=\"" + temp + "\" width=\"" + width + "\" height=\"60\" /></a>");
                    advertise.AdvertiseId = sb.ToString();
                    CMSModel.CMSEntities cms = DAL.DBManager.GetCmsEmtity();
                    if (!cms.Advertise.Any(x => x.AdvertiseId == txtId.Text))
                    {
                        cms.AddToAdvertise(advertise);
                        cms.SaveChanges();
                        ClientScript.RegisterStartupScript(GetType(), "save", "alert('添加成功');document.location='default.aspx'", true);
                    }
                    else
                    {
                        ClientScript.RegisterStartupScript(GetType(), "save", "alert('广告ID已存在');", true);
                    }
                }
                catch (Exception)
                {
                    ClientScript.RegisterStartupScript(GetType(), "save", "alert('添加失败,请重试');", true);
                }
            }
            else
            {
                ClientScript.RegisterStartupScript(GetType(), "save", "alert('请选择正确的文件格式!');", true);
            }
        }
        else
        {
            ClientScript.RegisterStartupScript(GetType(), "save", "alert('未选定上传图片或flash文件');", true);
        }
    }
}